package com.houserent.view;

import com.houserent.domain.House;
import com.houserent.service.HouseService;
import com.houserent.utils.Utility;

/**
 * @Author 小闽
 * @Date 2024/8/24 11:25
 * @PackageName: com.houserent.view
 * @ClassName: HouseView
 * @Description: 显示界面、接收用户输入、调用HouseService完成对房屋信息的各种操作
 * @Version 1.0
 */
public class HouseView {
    private boolean loop = true;
    private char key = ' ';
    private HouseService houseService = new HouseService(10);

    /**
     * 编写list方法显示房屋出租的信息
     */
    public void listHouse(){
        System.out.println("编号\t房主\t电话\t地址\t月租\t状态（未出租）/（已出租）");
           House[] house =  houseService.list();
        for (int i = 0; i < house.length; i++) {
            if (house[i] == null){
                break;
            }
            System.out.println(house[i]);
        }
    }

    /**
     * 编写addHouse()方法显示房屋增加信息
     */
    public void addHouse(){
        System.out.println("---------------------添加房屋------------------");
        System.out.print("姓名：");
        String name = Utility.readString(8);
        System.out.print("电话：");
        String phone = Utility.readString(3);
        System.out.print("地址：");
        String address = Utility.readString(3);
        System.out.print("月租：");
        int  monthRent = Utility.readInt();
        System.out.print("状态：（未出租/已出租）");
        String status = Utility.readString(3);
        House house = new House(0, name, phone, address, monthRent, status);
        if (houseService.add(house)){
            System.out.println("添加房屋成功！");
        }else{
            System.out.println("添加房屋失败！！！");
        }
    }

    /**
     * 编写delHouse()方法进行房屋的删除
     */
    public void delHouse(){
        System.out.println("-------------------删除房屋---------------------");
        System.out.println("请选择待删除房屋编号（-1退出）");
        int  id = Utility.readInt();
        if (id == -1){
            System.out.println("-----------------放弃删除房屋信息------------");
            return;
        }
//        System.out.println("确认是否删除（Y/N）：请小心选择：");
//        System.out.println("请输入你的选择（Y/N）");
//        String choice = Utility.readString(2);
        // 该方法本身就有循环判断的逻辑，必须输出Y/N
        char choice = Utility.readConfirmSelection();
        if (choice == 'Y' ){
            if (houseService.delete(id)){
                System.out.println("--------------房屋删除信息成功-------------");
            }else{
                System.out.println("--------------房屋编号不存在，房屋信息删除失败----------");
            }
        }else{
            System.out.println("-------------------放弃删除房屋信息-------------");
        }

    }
    /**
     * 编写selHouse()方法实现房屋信息查询
     */
    public void selHouse(){
        System.out.println("-----------------------查询房屋---------------------");
        System.out.println("请输入要查询的房屋编号：");
        int  id = Utility.readInt();
        House house = houseService.select(id);
        if (house != null){
            System.out.println("查询房屋信息成功！" +"\n"+ house);
        }else{
            System.out.println("房屋编号不存在，查询房屋信息失败！！！！");
        }
    }
    /**
     * 编写updateHouse()方法实现房屋的修改
     */
    public void updateHouse(){
        System.out.println("============================修改房屋信息=======================");
        System.out.println("请选择待修改的房屋编号（-1退出）");
        int updateId = Utility.readInt();
        if (updateId == -1){
            System.out.println("===========================放弃删除房屋编号================");
            return;
        }
        // 在这里返回的是引用类型[即：就是数组的元素]
        // 因此，在后面对house.setXxx(),就会修改HouseService中数组的元素！！！！！！！！！
        House house = houseService.select(updateId);

        if (house == null){
            System.out.println("=========================修改的房屋信息不存在================");
            return;
        }
        System.out.print("姓名("+house.getName()+")");
        String name = Utility.readString(8, "");
        if (!"".equals(name)){
            house.setName(name);
        }
        System.out.print("电话("+house.getPhone()+")");
        String phone = Utility.readString(12, "");
        if (!"".equals(phone)){
            house.setPhone(phone);
        }
        System.out.print("地址("+house.getAddress()+")");
        String address = Utility.readString(30,"");
        if (!"".equals(address)){
            house.setAddress(address);
        }
        System.out.print("租金("+house.getRent()+")");
        int rent = Utility.readInt();
        if (0 != rent){
            house.setRent(rent);
        }
        System.out.print("状态("+house.getState()+")");
        String status = Utility.readString(3,"");
        if (!"".equals(status)){
            house.setState(status);
        }
        System.out.println("============================修改房屋信息成功========================");
    }
    /**
     * 退出exit()方法
     */
    public void exit(){
        char c = Utility.readConfirmSelection();
        if(c == 'Y'){
            loop = false;
        }
    }
    public void mainMenu(){
        do {  
            System.out.println("================房屋出租系统====================");
            System.out.println("\t\t\t1 新 增 房 源");
            System.out.println("\t\t\t2 查 找 房 源");
            System.out.println("\t\t\t3 删 除 房 屋 信 息");
            System.out.println("\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t6 退       出");
            System.out.println("请输出你的选择：1-6");
            key = Utility.readChar();
            switch (key){
                case '1':
                    addHouse();
                    break;
                case  '2':
                    selHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    updateHouse();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    exit();
                    break;
            }
        }while(loop);
    }
}
